﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Business;
using System.Net.Mail;
using System.Net;


namespace MyWeb.Module.Contact
{
    public partial class Contact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lsvcontact.DataSource = ContactService.Contact_GetByTop("1","","Id asc");
                lsvcontact.DataBind();
            }
        }
        private static string MailBody(string name, string message, string diachi, string sdt)
        {
            string strHTML = "";
            strHTML += "Ho ten: " + name + "<br>";
            strHTML += "Địa chỉ: " + diachi + "<br>";
            strHTML += "Điện thoại: " + sdt + "<br>";            
            strHTML += "Nội dung:<br><b>";
            strHTML += message + "</b>";
            return strHTML;
        }
        public static bool Sendmail_Gmail(string to, string subject, string message, string name, string sdt)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(to);
            mail.Subject = subject;
            mail.IsBodyHtml = true;
            mail.Body = MailBody(subject, message, name, sdt);
            mail.From = new MailAddress("jackichanchan@gmail.com");
            try
            {
                SmtpClient client = new SmtpClient();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                NetworkCredential credentials = new NetworkCredential("dathangtranh@heaven.vn", "ac123456");
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;
                client.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        protected void btnsend_Click(object sender, EventArgs e)
        {
       
                bool success = Sendmail_Gmail("ordertranh@heaven.vn", txtname.Text, txtdes.Text, txtadress.Text, txtphone.Text);
                if (success)
                {
                    lblstatus.Text = "Gửi Email thành công";

                }
                else
                    lblstatus.Text = "Không gửi được";
           


        }
    }
}